/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

import xowl.gmi.environment.Locale;

/**
 *
 * @author Laurent WOUTERS
 */
public class DiagramNode extends WinProjectsNode {
    public static interface Actions {
        void onOpen();
        void onDelete();
    }
    
    private Actions actions;
    private boolean modified;
    private javax.swing.JPopupMenu menu;
    
    public void setModified(boolean modified) {
        this.modified = modified;
        if (this.win != null)
            this.win.repaint();
    }
    public javax.swing.JPopupMenu getMenu() { return menu; }
    
    public DiagramNode(Actions actions, Locale locale, String name, String viewName) {
        super("[" + viewName + "] " + name);
        this.actions = actions;
        this.menu = new javax.swing.JPopupMenu();
        javax.swing.JMenuItem itemShow = new javax.swing.JMenuItem(locale.popupDiagramOpen, IconProvider.getEmbedded("diagram.png"));
        itemShow.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { DiagramNode.this.actions.onOpen(); }
        });
        javax.swing.JMenuItem itemDelete = new javax.swing.JMenuItem(locale.popupDiagramDelete, IconProvider.getEmbedded("delete.png"));
        itemDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { DiagramNode.this.actions.onDelete(); }
        });
        this.menu.add(itemShow);
        this.menu.add(itemDelete);
    }
    
    @Override public java.awt.Component getRendering(ViewStyle style, boolean selected) {
        javax.swing.JLabel label = new javax.swing.JLabel(getUserObject().toString(), IconProvider.getEmbedded("diagram.png"), javax.swing.JLabel.TRAILING);
        label.setFont(new java.awt.Font(style.fontName, 0, style.fontSize));
        if (modified)
            label.setForeground(style.projectsModifiedColor);
        if (selected) {
            label.setOpaque(true);
            label.setBackground(style.projectsSelectedColor);
        }
        label.setComponentPopupMenu(menu);
        return label;
    }
}
